using System;
using System.Collections.Generic;
using System.Data;
using METU_CMT.DataSets.METUCMTDataSetTableAdapters;

namespace METU_CMT.Business
{
    public class TeamTableAdapter
    {
        public void GetTeam(int teamId)
        {
            //TODO: Not Implemented
        }

        public List<Deliverable> GetTeamDeliverables(int teamId)
        {
            List<Deliverable> _deliverables = new List< Deliverable >();

            DocumentsTableAdapter _documentsTableAdapter = new DocumentsTableAdapter();
            DataTable _deliverablesDt = _documentsTableAdapter.GetDataByTeamId(teamId);

            foreach (DataRow _deliverableDr in _deliverablesDt.Rows)
            {
                Deliverable _deliverable = new Deliverable();
                _deliverable.DeliverableId = int.Parse(_deliverableDr["DocumentId"].ToString());
                _deliverable.DocType = _deliverableDr["DocType"].ToString();
                _deliverable.Event = new Event(int.Parse(_deliverableDr["EventId"].ToString()));
                _deliverable.File = _deliverableDr["DocLink"].ToString();

                bool _successfull;
                DateTime _uploadTime = CommonMethods.ToDateTime(_deliverableDr["UploadTime"].ToString(), "dd.MM.yyyy HH:mm:ss", out _successfull);

                if (_successfull == false)
                {
                    return null;
                }

                _deliverable.UploadTime = _uploadTime;
                _deliverable.SubmitterId = int.Parse(_deliverableDr["UploaderId"].ToString());
                _deliverables.Add(_deliverable);
            }
            return _deliverables;
        }

        public List<Student> GetMembers(int teamId)
        {
            List<Student> _students = new List< Student >();

            TeamMembersTableAdapter _teamMembersTableAdapter = new TeamMembersTableAdapter(  );
            DataTable _membersDt = _teamMembersTableAdapter.GetDataByTeamId( teamId );
            foreach ( DataRow _memberDr in _membersDt.Rows )
            {
                int _studentId;
                if(int.TryParse( _memberDr["UserId"].ToString(  ), out _studentId ) == false)
                {
                    
                }
                Student _student = new Student( _studentId );
                _students.Add( _student );
            }
            return _students;
        }

        public string GetLogoFile(int teamId)
        {
            TeamsTableAdapter _teamsTableAdapter = new TeamsTableAdapter(  );
            DataTable _teamsDt = _teamsTableAdapter.GetDataByTeamId( teamId );
            if(_teamsDt.Rows.Count < 1)
            {
                //TODO: Log: No Team
                return string.Empty;
            }
            return _teamsDt.Rows[ 0 ][ "TeamLogo" ].ToString(  );
        }

        public string GetTeamName(int teamId)
        {
            TeamsTableAdapter _teamsTableAdapter = new TeamsTableAdapter();
            DataTable _teamsDt = _teamsTableAdapter.GetDataByTeamId(teamId);
            if (_teamsDt.Rows.Count < 1)
            {
                //TODO: Log: No Team
                return string.Empty;
            }
            return _teamsDt.Rows[ 0 ][ "TeamName" ].ToString( );
        }

        public void UploadDeliverable(int teamId, Deliverable deliverable, int userId)
        {
            DocumentsTableAdapter _documentsTableAdapter = new DocumentsTableAdapter(  );
            _documentsTableAdapter.InsertDocument( deliverable.Event.EventId, teamId, deliverable.DocType,
                                                   deliverable.UploadTime, deliverable.File, userId );
        }

        public void RemoveStudent(int teamId, Student student)
        {
            TeamMembersTableAdapter _teamMembersTableAdapter = new TeamMembersTableAdapter(  );
            _teamMembersTableAdapter.DeleteTeamMember( teamId, student.UserId );
        }
    }
}